package com.cmccgz.udacs_landlord_webapp.main.accreditmanager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.baselibrary.utils.ToastUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.bean.CardInfo;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;

/**
 * Created by June on 2017-10-31.
 */

public class AddCardActivity extends BaseActivity implements View.OnClickListener, AddCardContract.View {
    public final static String KEY_CARD_RESULT="key_card_result";
    private AddCardContract.Presenter mPresenter;
    private ViewHelper mViewHelper;
    private LinearLayout ll_cardtype;
    private TextView tv_get_cardno;

    private String houseId;
    private String houseName;
    private long serverTime;
    private CardInfo cardInfo;
    private TextView tv_card_type;
    private TextView tv_card_no;
    private TextView btn_complete;
    private AlertDialog selectTypeDialog;
    private String type="1";//1 ：身份证 2：IC卡
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.ll_cardtype:
                if (selectTypeDialog==null) {
                    selectTypeDialog = new AlertDialog.Builder(mContext)
                            .setTitle("选择类型")
                            .setItems(new String[]{"身份证", "IC卡"}, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    switch (which) {
                                        case 0:
                                            type = "1";
                                            break;
                                        case 1:
                                            type = "2";
                                            break;
                                    }
                                    asyTypeView();
                                }
                            }).create();
                }
                selectTypeDialog.show();
                break;
            case R.id.tv_card_no:
            case R.id.tv_get_cardno:
                showLoadingDialog("正在获取卡号...");
                long time = StringUtils.isEmpty(tv_card_no.getText().toString()) ? this.serverTime:-1;
                mPresenter.getCardInfo(AppConfig.getMobile(this), houseId, time);
                break;
            case R.id.btn_complete:
                ToastUtils.show(mContext,"新增卡片成功!");
                Intent data = new Intent();
                data.putExtra(KEY_CARD_RESULT,cardInfo);
                setResult(RESULT_OK,data);
                finish();

            /*    String mobile=AppConfig.getMobile(mContext);
                String uuid="";
                String cardType="";
                if (cardInfo!=null) {
                    uuid=cardInfo.getUuid();
                    cardType=cardInfo.getCardType();
                }
                mPresenter.addCard(mobile,houseId,cardType,uuid);*/
                break;
            default:
                break;
        }
    }

    private void asyTypeView() {
        String typeName;
        switch (type) {
            case "1":
                typeName="身份证";
                break;
            case "2":
                typeName="IC卡";
                break;
            default:
                typeName="未知";
        }
        tv_card_type.setText(typeName);
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_card_add;
    }

    @Override
    protected void initView() {
        mPresenter = new AddCardPresenter(this);
        ll_cardtype = (LinearLayout) findViewById(R.id.ll_cardtype);
        tv_get_cardno = (TextView) findViewById(R.id.tv_get_cardno);
        tv_card_type = ((TextView) findViewById(R.id.tv_card_type));
        tv_card_no = ((TextView) findViewById(R.id.tv_card_no));
        btn_complete = ((TextView) findViewById(R.id.btn_complete));
        ll_cardtype.setOnClickListener(this);
        tv_get_cardno.setOnClickListener(this);
        tv_card_no.setOnClickListener(this);
        btn_complete.setOnClickListener(this);
        initData();
        getServerTime();
        asyTypeView();
    }

    private void initData() {
        Intent intent = getIntent();
        houseId = intent.getStringExtra(PersonListActivity.HOUSE_ID);
        houseName = intent.getStringExtra(PersonListActivity.HOUSE_NAME);
    }

    @Override
    protected void initFragment() {

    }

    @Override
    protected void beforePermission() {
        mViewHelper = ViewHelper.instance(mContext);
        if (mViewHelper != null) {
            mViewHelper.setTitle("新增卡片");
            mViewHelper.setAllVisable(false);
            mViewHelper.setBackTextVisable(true);
        }
    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    @Override
    public void setPresenter(AddCardContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void setServerTime(long serverTime) {
        dismissLoadingDialog();
        if (serverTime>0) {
            this.serverTime = serverTime;
            ToastUtils.show(mContext,"获取服务器时间成功，请按照指示进行后续操作.");
        }else{
            showConfirmDialog("提示", "获取服务器时间失败，是否重新获取？", "确认", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    getServerTime();
                }
            }, "退出", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    finish();
                }
            },false);
        }
    }

    @Override
    public void showCardNo(CardInfo cardInfo) {
        dismissLoadingDialog();
        if (cardInfo==null) {
            ToastUtils.show(mContext,"获取失败，请稍后再试!");
            return;
        }
        this.cardInfo = cardInfo;
        this.cardInfo.setCardType(type);
        tv_card_no.setText(cardInfo.getUuid());
        asyTypeView();
        ToastUtils.show(mContext,"获取卡号成功!");
    }

    @Override
    public void showCardError(String msg) {
        dismissLoadingDialog();
        ToastUtils.show(mContext,msg);
    }

/*    @Override
    public void addCardSuccess() {
        dismissLoadingDialog();
        ToastUtils.show(mContext,"新增卡片成功!");
        Intent data = new Intent();
        data.putExtra(KEY_CARD_RESULT,cardInfo);
        setResult(RESULT_OK,data);
    }

    @Override
    public void addCardFail(String message) {
        ToastUtils.show(mContext,message);
        dismissLoadingDialog();
    }*/

    private void getServerTime(){
        showLoadingDialog("正在获取服务器时间...");
        mPresenter.getServerTime();
    }

}
